package org.example.Mapper;

import org.apache.ibatis.annotations.*;
import org.example.domain.Activity;

import java.util.Date;

@Mapper
public interface ActivityMapper {

    @Select("SELECT * FROM activity WHERE id=#{id} LIMIT 1;")
    Activity getAllById(Integer id);

    @Insert("INSERT INTO activity (name,date,address,picture,content,max,stillHave,emcee,theme,emceeInfo,screenType,tag)" +
            "VALUES (#{name},#{date},#{address},#{picture},#{content},#{max},#{stillHave},#{emcee},#{theme}," +
            "#{emceeInfo},#{screenType},#{tag});")
    void addActivity(@Param("name") String name,@Param("date") Date date,@Param("address") String address,
                     @Param("picture") byte[] picture,@Param("content") String content,@Param("max") Integer max,
                     @Param("stillHave") Integer stillHave,@Param("emcee") String emcee,@Param("theme") String theme,
                     @Param("emceeInfo") String emceeInfo,@Param("screenType") Integer screenType,
                     @Param("tag") Integer tag);//上架一项活动

    @Select("SELECT * FROM activity WHERE isOver=0 ORDER BY date DESC LIMIT #{start},3;")
    Activity[] getActivitiesByIsOver(int start);//获取预告中的活动

    @Select("SELECT * FROM activity WHERE name LIKE #{name};")
    Activity[] getActivitiesByName(String name);

    @Select("<script>SELECT id,name,date,address,isOver,content,max,stillHave,emcee,theme,emceeInfo,isSignIn,tag FROM" +
            " activity WHERE name LIKE #{name} " +
            "<if test=\"tag!=null\"> AND tag=#{tag} </if></script>")
    Activity[] getActivitiesByNameButNoPicture(@Param("name") String name,
                                               @Param("tag") Integer tag);//根据名字查询活动并且不返回图片，优化查询速度

    @Select("SELECT * FROM activity WHERE name LIKE #{name} AND isOver=0;")
    Activity[] getActivitiesByNameAndIsOver(String name);

    @Update("UPDATE activity SET isOver=1 WHERE id=#{id};")
    void downActivityById(Integer id);

    @Update("UPDATE activity SET name=#{name},content=#{content},emcee=#{emcee},theme=#{theme},emceeInfo=#{emceeInfo}" +
            ",tag=#{tag} WHERE id=#{id};")
    void updateActivityInfoById(@Param("name") String name,@Param("content") String content,@Param("id") Integer id,
                                @Param("emcee") String emcee,@Param("theme") String theme,
                                @Param("emceeInfo") String emceeInfo,@Param("tag") Integer tag);//根据id更新活动信息

    @Update("UPDATE activity SET stillHave=stillHave-1 WHERE id=#{id};")
    void reduceStillHaveById(Integer id);//减少指定id活动的报名余额

    @Update("UPDATE activity SET stillHave=stillHave+1 WHERE id=#{id};")
    void addStillHaveById(Integer id);//增加知指定id活动的报名余额

    @Update("UPDATE activity SET isSignIn=1 WHERE id=#{id};")
    void changeIsSignInById(Integer id);//修改指定id活动为已签到
}
